<?php
declare (strict_types=1);

namespace app\organization\controller\core;

use app\common\controller\Frontend;
use app\common\model\banxi\core\CoreClassHourDetail as CoreClassHourDetailModel;
use app\common\model\banxi\organization\Organization as OrganizationModel;
use app\common\service\ExportService;
use think\db\exception\DbException;

class CoreClassHourDetail extends Frontend
{
    protected array $noNeedLogin = [];
    protected array $noNeedPermission = [];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new CoreClassHourDetailModel();
    }


    /**
     * 分页查询
     * @return void
     * @throws DbException
     */
    public function listPage(): void
    {
        $param = $this->request->param(['index', 'organizationId', 'studentId', 'limit', 'orderBy', "query"]);
        $organizationId = $param['organizationId'];
        $studentId = $param['studentId'];
        //查询条件解析
        $where = get_query($param);
        if (!empty($organizationId)) {
            $where[] = ['payerOrganizationId|receiverOrganizationId', '=', $organizationId];
        }
        if ($studentId) {
            $where[] = ['payerStudentId|receiverStudentId', '=', $studentId];
        }
        $organizationId = $this->auth->organizationId;
        $organizationModel = new OrganizationModel();
        $organizationIds = $organizationModel->getSonOrganization($organizationId);
        $where[] = ['payerRealOrganizationId|receiverOrganizationId', 'in', $organizationIds];

        //获取分页数据
        $pageParams = get_page_params($param);
        $list = $this->model->where($where)
            ->order(['id' => 'desc'])
            ->paginate([
                'page' => $pageParams['page'],
                'list_rows' => $pageParams['limit'],
            ]);
        $list = result_row($list);
        $list['rows'] = $this->model->wrapperAdminDBList($list['rows']);
        $this->ok('操作成功', $list);
    }

    /**
     * 查询
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get()
    {
        $param = $this->request->param(["query" => []]);
        if (empty($param['query'])) {
            $this->fail('请传递你要查询的记录');
        }
        $where = get_query($param);
        $info = $this->model->where($where)->find();
        if (empty($info)) {
            $this->fail('记录不存在');
        }
        $info = $this->model->wrapperAdminDB($info);
        $this->ok('操作成功', $info);
    }

    public function exportExcel()
    {
        $param = $this->request->param(['index', 'organizationId', 'studentId', 'limit', 'orderBy', "query"]);
        $organizationId = $param['organizationId'];
        $studentId = $param['studentId'];
        //查询条件解析
        $where = get_query($param);
        if (!empty($organizationId)) {
            $where[] = ['payerOrganizationId|receiverOrganizationId', '=', $organizationId];
        }
        if ($studentId) {
            $where[] = ['payerStudentId|receiverStudentId', '=', $studentId];
        }
        $organizationId = $this->auth->organizationId;
        $organizationModel = new OrganizationModel();
        $organizationIds = $organizationModel->getSonOrganization($organizationId);
        $where[] = ['payerRealOrganizationId|receiverOrganizationId', 'in', $organizationIds];

        $list = $this->model
            ->where($where)
            ->order(['id' => 'asc'])
            ->field('id,payerOrganizationId,payerStudentId,receiverStudentId,receiverOrganizationId,payerAccountType,receiverAccountType,businessType,payAmount,payType,classHourType,classHourNum,describe,status')
            ->select();
        if (!count($list)) {
            $this->fail('导出失败，没有数据');
        }
        $list = $this->model->wrapperAdminDBList($list);
        $exportService = new ExportService();
        $exportService->coreClassHourDetailExport($list);
    }
}
